"""
应用核心配置
基于Pydantic Settings的配置管理
"""
from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict
from typing import List, Optional
import secrets


class Settings(BaseSettings):
    """应用配置类"""
    model_config = SettingsConfigDict(
        env_file=".env",
        env_file_encoding="utf-8",
        case_sensitive=True
    )
    
    # 基础配置
    PROJECT_NAME: str = "SmartCity-LLM Trainer"
    ENVIRONMENT: str = Field(default="development", description="运行环境")
    DEBUG: bool = Field(default=True, description="调试模式")
    API_V1_STR: str = "/api/v1"
    
    # 安全配置
    SECRET_KEY: str = Field(default_factory=lambda: secrets.token_urlsafe(32))
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8  # 8天
    REFRESH_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 30  # 30天
    
    # CORS配置
    ALLOWED_HOSTS: List[str] = ["*"]
    
    # 数据库配置
    DATABASE_URL: str = Field(
        default="postgresql+asyncpg://smartcity:smartcity123@localhost:5432/smartcity_llm",
        description="数据库连接URL"
    )
    
    # Redis配置
    REDIS_URL: str = Field(
        default="redis://localhost:6379/0",
        description="Redis连接URL"
    )
    
    # Celery配置
    CELERY_BROKER_URL: str = Field(
        default="redis://localhost:6379/1",
        description="Celery消息代理URL"
    )
    CELERY_RESULT_BACKEND: str = Field(
        default="redis://localhost:6379/2",
        description="Celery结果后端URL"
    )
    
    # 文件存储配置
    UPLOAD_PATH: str = Field(default="./uploads", description="文件上传路径")
    MAX_UPLOAD_SIZE: int = Field(default=100 * 1024 * 1024, description="最大上传文件大小(100MB)")
    
    # 外部服务配置
    MINIO_ENDPOINT: str = Field(default="localhost:9000", description="MinIO服务地址")
    MINIO_ACCESS_KEY: str = Field(default="minioadmin", description="MinIO访问密钥")
    MINIO_SECRET_KEY: str = Field(default="minioadmin", description="MinIO密钥")
    MINIO_BUCKET_NAME: str = Field(default="smartcity-models", description="MinIO存储桶名称")
    
    # 监控配置
    PROMETHEUS_ENABLED: bool = Field(default=True, description="启用Prometheus监控")
    LOG_LEVEL: str = Field(default="INFO", description="日志级别")
    
    # AI训练配置
    TRAINING_WORKSPACE: str = Field(default="./training_workspace", description="训练工作空间")
    MAX_CONCURRENT_TRAINING_JOBS: int = Field(default=3, description="最大并发训练任务数")
    
    # GPU配置
    CUDA_VISIBLE_DEVICES: Optional[str] = Field(default=None, description="可见GPU设备")
    
    # 联邦学习配置
    FEDERATED_LEARNING_ENABLED: bool = Field(default=True, description="启用联邦学习")
    MAX_FEDERATION_PARTICIPANTS: int = Field(default=10, description="最大联邦学习参与者数量")


# 创建全局配置实例
settings = Settings()